package leo.game.ui
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.text.TextField;
	
	import ghostcat.util.easing.TweenUtil;
	
	import leo.data.GameConfig;
	import leo.data.GameConst;
	import leo.data.GameModel;
	import leo.data.TowerVO;
	import leo.game.stage.Game;
	import leo.manager.McMannager;
	import leo.manager.SoundManager;
	import leo.manager.VOManager;
	
	public class UIBottom extends Sprite
	{
		private static var _instance :UIBottom = null;
		public var bottomUI :MovieClip;
		
		/**
		 * wall 墙
		 */		
		public var btnWall :MovieClip;
		/**
		 *  topSpeed 高速塔
		 */		
		public var btnTSTower :MovieClip;
		/**
		 * multiTarget 群攻塔
		 */		
		public var btnMTTower :MovieClip;
		/**
		 *  negativeEffects 减速塔
		 */		
		public var btnNETower :MovieClip;
		/**
		 * hightAttack 高攻塔
		 */		
		public var btnHTTower :MovieClip;
		/**
		 * AcceptBattle 迎战
		 */		
		public var btnAcceptBattle :MovieClip;
		/**
		 * cancel  取消
		 */		
		public var btnCancel :MovieClip;
		/**
		 * 拆除 
		 */		
		public var btnRemove :MovieClip;
		/**
		 * 移动 
		 */		
		public var btnMove :MovieClip;
		/**
		 * 是否已经开始迎战 
		 */		
		public var isAcceptBattle :Boolean;
		/**
		 * 怪物信息 
		 */		
		public var txtMsg :TextField;
		public function UIBottom()
		{
			super();
			init();
		}
		
		public static function get instance():UIBottom
		{
			if(_instance == null)
			{
				_instance = new UIBottom();
			}
			return _instance;
		}
		/**
		 * 显示底部UI 
		 * @param duration
		 * @param tx
		 * @param ty
		 * 
		 */		
		public function showBottom(duration :int,tx :int,ty :int):void
		{
			TweenUtil.to(this,duration,{x :tx ,y :ty});
		}
		/**
		 * 重置按钮 
		 * 
		 */		
		public function resetBTN():void
		{
			this.isAcceptBattle = false;
			this.btnWall.transform.colorTransform = new ColorTransform();
			this.btnAcceptBattle.transform.colorTransform = new ColorTransform();
			this.btnCancel.transform.colorTransform = new ColorTransform();
			this.btnHTTower.transform.colorTransform = new ColorTransform();
			this.btnMTTower.transform.colorTransform = new ColorTransform();
			this.btnNETower.transform.colorTransform = new ColorTransform();
			this.btnTSTower.transform.colorTransform = new ColorTransform();
			this.btnRemove.transform.colorTransform = new ColorTransform();
			this.btnMove.transform.colorTransform = new ColorTransform();
			this.txtMsg.text = "";
		}
		
		private function init():void
		{
			var name :String = GameConst.BOTTOM_UI;
			bottomUI = McMannager.instance.getMCByName(name);
			this.addChild(bottomUI);
			initBtn();
		}
		
		
		private function initBtn():void
		{
			this.btnWall = bottomUI.btnWall;
			this.btnAcceptBattle = bottomUI.btnAcceptBattle;
			this.btnHTTower = bottomUI.btnHTTower;
			this.btnMTTower = bottomUI.btnMTTower;
			this.btnNETower = bottomUI.btnNETower;
			this.btnTSTower = bottomUI.btnTSTower;
			this.btnCancel = bottomUI.btnCancel;
			this.btnRemove = bottomUI.btnRemove;
			this.btnMove = bottomUI.btnMove;
			this.txtMsg = bottomUI.txtMsg;
			this.txtMsg.text = "";
			addEventListener(MouseEvent.CLICK,mouseClickHandler);
			addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
		}
		
		private function mouseClickHandler(event :MouseEvent):void
		{
			if(this.isAcceptBattle) return;
			
			var name :String;
			switch(event.target)
			{
				case this.btnAcceptBattle:
					if(UIMap.instance.isRemove || UIMap.instance.isMove) return;
					trace("btnAcceptBattle");
					UIMap.instance.startBattle();
					this.btnAcceptBattle.filters = null;
					changeBtnColorTransform();
					this.isAcceptBattle = true;
					UITooltip.instance.hideTooltip();
					this.txtMsg.text = "当前第" + 
										(GameModel.instance.battleNumber + 1) + 
										"波怪物，剩余" + 
										(GameConfig.monster_list.length - (GameModel.instance.battleNumber + 1)) + 
										"波";
										
					SoundManager.instance.play("FightMusic",-1);
					break;
				case this.btnHTTower:
					if(UIMap.instance.isRemove || UIMap.instance.isMove) return;
					UIMap.instance.isAddNewTower = true;
					name = GameConst.MC_HIGHTATTACKTOWER;
					UIMap.instance.addNewTower(name);
					UITooltip.instance.hideTooltip();
					trace("btnHTTower");
					break;
				case this.btnMTTower:
					if(UIMap.instance.isRemove || UIMap.instance.isMove) return;
					UIMap.instance.isAddNewTower = true;
					name = GameConst.MC_MULTITARGETTOWER;
					UIMap.instance.addNewTower(name);
					UITooltip.instance.hideTooltip();
					trace("btnMTTower");
					break;
				case this.btnNETower:
					if(UIMap.instance.isRemove || UIMap.instance.isMove) return;
					UIMap.instance.isAddNewTower = true;
					name = GameConst.MC_NEGATIVEEFFECTSTOWER;
					UIMap.instance.addNewTower(name);
					UITooltip.instance.hideTooltip();
					trace("btnNETower");
					break;
				case this.btnTSTower:
					if(UIMap.instance.isRemove || UIMap.instance.isMove) return;
					UIMap.instance.isAddNewTower = true;
					name = GameConst.MC_TOPSPEEDTOWER;
					UIMap.instance.addNewTower(name);
					UITooltip.instance.hideTooltip();
					trace("btnTSTower");
					break;
				case this.btnWall:
					if(UIMap.instance.isRemove || UIMap.instance.isMove) return;
					UIMap.instance.isAddNewTower = true;
					name = GameConst.MC_WALL;
					UIMap.instance.addNewTower(name);
					UITooltip.instance.hideTooltip();
					trace("wall");
					break;
				case this.btnCancel:
					trace("cancel");
					UIMap.instance.cancel();
					UITooltip.instance.hideTooltip();
					break;
				case this.btnRemove:
					trace("btnRemove");
					if(UIMap.instance.isMove)
					{
						UIMap.instance.isMove = false;
						this.btnMove.filters = null;
					}
					if(UIMap.instance.isRemove)
					{
						UIMap.instance.isRemove = false;
						this.btnRemove.filters = null;
					}else
					{
						UIMap.instance.isRemove = true;
						this.btnRemove.filters = [GameConst.BOTTOM_MOUSE_SELECTED];
					}
					UITooltip.instance.hideTooltip();
					break;
				case this.btnMove:
					trace("btnMove");
					if(UIMap.instance.isRemove)
					{
						UIMap.instance.isRemove = false;
						this.btnRemove.filters = null;
					}
					if(UIMap.instance.isMove)
					{
						UIMap.instance.isMove = false;
						this.btnMove.filters = null;
					}else
					{
						UIMap.instance.isMove = true;
						this.btnMove.filters = [GameConst.BOTTOM_MOUSE_SELECTED];
					}
					break;
				default :
					break;
			}
		}
		/**
		 * 設置按鈕濾鏡 
		 * 
		 */		
		private function changeBtnColorTransform():void
		{
			this.btnWall.transform.colorTransform = new ColorTransform(0.4,0.3,0.4,1,30,40,30,0);
			this.btnAcceptBattle.transform.colorTransform = new ColorTransform(0.4,0.3,0.4,1,30,40,30,0);
			this.btnCancel.transform.colorTransform = new ColorTransform(0.4,0.3,0.4,1,30,40,30,0);
			this.btnHTTower.transform.colorTransform = new ColorTransform(0.4,0.3,0.4,1,30,40,30,0);
			this.btnMTTower.transform.colorTransform = new ColorTransform(0.4,0.3,0.4,1,30,40,30,0);
			this.btnNETower.transform.colorTransform = new ColorTransform(0.4,0.3,0.4,1,30,40,30,0);
			this.btnTSTower.transform.colorTransform = new ColorTransform(0.4,0.3,0.4,1,30,40,30,0);
			this.btnRemove.transform.colorTransform = new ColorTransform(0.4,0.3,0.4,1,30,40,30,0);
			this.btnMove.transform.colorTransform = new ColorTransform(0.4,0.3,0.4,1,30,40,30,0);
		}
		
		private function mouseMoveHandler(event :MouseEvent):void
		{
			//已经开始迎战不能建造塔
			if(isAcceptBattle)
			{
				return;
			}
			var target :MovieClip  = event.target as MovieClip;
			var towerVO :TowerVO
			//墙
			if(this.btnWall.hitTestPoint(stage.mouseX,stage.mouseY))
			{
				this.btnWall.filters = [GameConst.BOTTOM_MOUSE_ON_TOWER];
				towerVO = VOManager.instance.getTowerVOByName(GameConst.MC_WALL);
				UITooltip.instance.showTooltip(stage.mouseX,stage.mouseY,towerVO);
			}else if(this.btnHTTower.hitTestPoint(stage.mouseX,stage.mouseY))
			{
				towerVO = VOManager.instance.getTowerVOByName(GameConst.MC_HIGHTATTACKTOWER);
				UITooltip.instance.showTooltip(stage.mouseX,stage.mouseY,towerVO);
				this.btnHTTower.filters = [GameConst.BOTTOM_MOUSE_ON_TOWER];
			}else if(this.btnMTTower.hitTestPoint(stage.mouseX,stage.mouseY))
			{
				towerVO = VOManager.instance.getTowerVOByName(GameConst.MC_MULTITARGETTOWER);
				UITooltip.instance.showTooltip(stage.mouseX,stage.mouseY,towerVO);
				this.btnMTTower.filters = [GameConst.BOTTOM_MOUSE_ON_TOWER];
			}else if(this.btnNETower.hitTestPoint(stage.mouseX,stage.mouseY))
			{
				towerVO = VOManager.instance.getTowerVOByName(GameConst.MC_NEGATIVEEFFECTSTOWER);
				UITooltip.instance.showTooltip(stage.mouseX,stage.mouseY,towerVO);
				this.btnNETower.filters = [GameConst.BOTTOM_MOUSE_ON_TOWER];
			}else if(this.btnTSTower.hitTestPoint(stage.mouseX,stage.mouseY))
			{
				towerVO = VOManager.instance.getTowerVOByName(GameConst.MC_TOPSPEEDTOWER);
				UITooltip.instance.showTooltip(stage.mouseX,stage.mouseY,towerVO);
				this.btnTSTower.filters = [GameConst.BOTTOM_MOUSE_ON_TOWER];
			}else if(this.btnAcceptBattle.hitTestPoint(stage.mouseX,stage.mouseY))
			{
				UITooltip.instance.showTooltip(stage.mouseX,stage.mouseY,null,"开始迎战");
				this.btnAcceptBattle.filters = [GameConst.BOTTOM_MOUSE_ON_TOWER];
			}else if(this.btnCancel.hitTestPoint(stage.mouseX,stage.mouseY))
			{
				UITooltip.instance.showTooltip(stage.mouseX,stage.mouseY,null,"取消建造");
				this.btnCancel.filters = [GameConst.BOTTOM_MOUSE_ON_TOWER];
			}else if(this.btnRemove.hitTestPoint(stage.mouseX,stage.mouseY))
			{
				UITooltip.instance.showTooltip(stage.mouseX,stage.mouseY,null,"点击拆除");
//				this.btnRemove.filters = [GameConst.BOTTOM_MOUSE_ON_TOWER];
			}else if(this.btnMove.hitTestPoint(stage.mouseX,stage.mouseY))
			{
				UITooltip.instance.showTooltip(stage.mouseX,stage.mouseY,null,"点击移动");
//				this.btnMove.filters = [GameConst.BOTTOM_MOUSE_ON_TOWER];
			}else
			{
				this.btnAcceptBattle.filters = 
					this.btnHTTower.filters = 
					this.btnMTTower.filters = 
					this.btnNETower.filters = 
					this.btnTSTower.filters = 
					this.btnWall.filters = 
//					this.btnRemove.filters = 
//					this.btnMove.filters =
					this.btnCancel.filters = null;
				UITooltip.instance.hideTooltip();
			}
		}
		/**
		 * 取消建造防守塔
		 */		
		private function cancel():void
		{
			UIMap.instance.isAddNewTower = false;
		}
		
		
	}
}